import {
	request
} from '@/api/request/index.js'
import uniLocation from '@/components/tabbar/index/auto-location.vue'
import share from '@/components/share.vue'
import initloaction_api from '@/api/map/init.js'
import local_addr from '@/util/address/local_addr.js'

export default {
	components: {
		uniLocation,
		share
	},
	data() {
		return {
			swiperList: [],
			iconList: [],
			threeList: [],
			otherList: [],
			showAddrtext: '重新定位',
			isMpweixin: false,
			staticIconlist: [
				this.$resBaseurl + '/static/tabbar/index/coordinates.png',
				this.$resBaseurl + '/static/tabbar/index/enter.png',
				this.$resBaseurl + '/static/tabbar/index/customerservice.png',
				this.$resBaseurl + '/static/tabbar/index/feedback.png'
			],
			explain: this.$resBaseurl + '/act/mrhappy/explain.png',
			moreurl: '',
			shareConfig:{
				Title:'嗨焙鲜生',
				Contents:'嗨焙鲜生',
				ShareLink:this.$currentlocation.getCurrentPageUrlWithArgs(),
				ImgsrcMp:''
			}
		}
	},
	onShow() {
		this.$setTitle("嗨焙鲜生·精选")
		this.initLoaction()
	},
	onLoad() {
		// #ifdef H5
		this.isMpweixin = false
		this.initShare()
		// #endif
		// #ifdef MP-WEIXIN
		this.isMpweixin = true
		// #endif
	},
	onPullDownRefresh() {
		this.initLoaction()
	},
	//微信小程序分享
	onShareAppMessage: function(res) {
		if (res.from === 'button') {}
		return {
			title: '嗨焙先生',
			path: this.$currentlocation.getCurrentPageUrlWithArgs(),
			imageUrl:this.$resBaseurl+'/m-images/happy/share/index.jpg?v='+(new Date().getHours()),
			success: function(res) {
				console.log('成功', res);
			}
		};
	},
	methods: {
		initLoaction(reload = false) {
			let that = this
			let p = {
				start() {
					that.showAddrtext = '正在定位...'
				},
				success(addr) {
					uni.stopPullDownRefresh();
					that.showAddrtext = addr.title || addr.city || '重新定位'
					that.checkAddress(addr, function(storeName) {
						that.indexData('上海', that.isMpweixin ? 'MP' : 'H5', storeName)
					})

				},
				fail() {
					uni.stopPullDownRefresh();
					that.indexData('上海', that.isMpweixin ? 'MP' : 'H5')
					that.showAddrtext = '重新定位'
					uni.showModal({
						title: '地址定位失败',
						content: '未获取到当前定位地址，请手动添加，谢谢',
						showCancel: false,
						confirmText: '去手动添加',
						confirmColor: '#e64340',
						success() {
							uni.navigateTo({
								url: '../../pages/searchaddress/searchaddress'
							})
						}
					})
				}
			}

			initloaction_api.initMyLocation(reload, p)
		},
		checkAddress(addr, callback) {
			let lat = addr.lat,
				lng = addr.lng,
				that = this;

			if (addr.store && callback) {
				callback(addr.store);
				return;
			}
			uni.showLoading({
				mask: true,
				title: '正在加载模块...'
			});
			request.get({
					data: {
						c: 'AddressController',
						m: 'CheckAddress',
						city: '上海',
						lat: lat,
						lng: lng
					}
				})
				.then(res => {
					if (res.Status == "ok") {
						let store = res.Tag
						addr.store = store;
						if (callback) callback(store)
						local_addr.setAddr(addr)
					} else {
						uni.showModal({
							title: '无法配送',
							content: '当前地址暂未开通配送，请更新其他地址',
							confirmText: '去修改',
							confirmColor: '#e64340',
							success(res) {
								if (res.confirm)
									uni.navigateTo({
										url: '../../pages/searchaddress/searchaddress'
									})
							}
						})
						if (callback) callback()
					}
				})
				.catch(error => {
					console.error(error)
					if (callback) callback()
				});
		},
		//初始化swiper数据
		indexData(city, channel_from, storeName) {
			let that = this;
			// uni.showLoading({
			// 	mask: false,
			// 	title: '正在加载模块...'
			// });
			request.get({
					data: {
						c: 'SwiperController',
						m: 'IndexData',
						station: city,
						channel_from: channel_from,
						store: storeName || ''
					}
				})
				.then(res => {
					uni.hideLoading()
					if (res.Status == "ok") {
						let result = res.Tag.result;
						let listIndex = res.Tag.resultIndex || [];

						that.swiperList = result["index_data_0"] || [];
						that.iconList = result["index_data_1"] || [];
						that.threeList = result["index_data_2"] || [];
						that.otherList = result["index_data_3"] || [];
						that.moreurl = res.Tag.moreurl;
					}
				})
				.catch(error => {

				});
		},
		gotoUrl(data) {

		},
		tosearchaddress() {
			uni.navigateTo({
				url: '/pages/searchaddress/searchaddress'
			})
		},
		topay() {
			let from_uri = '',
				to_uri = '/pages/weixin/weixin?orderid=11111111111111'
			uni.navigateTo({
				url: '/pages/weixin/redirect?to_uri=' + encodeURIComponent(to_uri)
			})
		},
		towebview() {
			uni.navigateTo({
				url: '/pages/webview/webview?redirectUrl=' + encodeURIComponent(
					'https://happycs.bestcake.com/static/h5/index.html')
			})
		},
		toshare() {
			this.$refs.sharesdk.toggleMask();
		},
		initShare() {
			let that =this
			this.$ShareSdk.Share({
				title: "嗨焙鲜生",
				link: that.$currentlocation.getCurrentPageUrlWithArgs(),
				imgUrl:that.$resBaseurl+'/m-images/happy/share/index.jpg?v='+(new Date().getHours()),
				success() {
					
				}
			})
		},
		navigatorTo(href) {
			if (href.indexOf('/pages/tabBar/') > -1) {
				uni.switchTab({
					url: href
				})
			} else
				uni.navigateTo({
					url: href
				})
		},
	}
}
